package directive;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modele.beans.Employee;
import modele.dao.EmployeeDAO;
import modele.form.EmployeeForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class EmployeeAction extends GlobalAction {

	private EmployeeDAO empDAO;

	@Override
	public String executeAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		empDAO = new EmployeeDAO(this.conn);
		String type = (String) request.getParameter("type");
		String ac = ACTION_FAIL;

		if (type.equals("Ajouter"))
			ac = AddEmployee(mapping, form);
		else if (type.equals("Modifier"))
			ac = UpdateEmployee(mapping, form);
		else if (type.equals("Supprimer"))
			ac = DeleteEmployee(mapping, form);
		else
			ac = ACTION_SUCCESS;
		
		EmployeeForm employee = (EmployeeForm) form;
		
		if (ac.equals(ACTION_SUCCESS)) {
			employee.setName("");
			employee.setFirstName("");
			employee.setPhoneNumber("");
			employee.setId("");
			employee.setAddress("");
		}
	
		employee.setEmployees(GetEmployees());
		request.setAttribute("EmployeeForm", employee);
		
		return ac;
	}

	private String AddEmployee(ActionMapping mapping, ActionForm form) {
		EmployeeForm employee = (EmployeeForm) form;

		Employee emp = new Employee(employee.getName(),
				employee.getFirstName(), employee.getAddress(),
				employee.getPhoneNumber(), employee.getId());
		try {
			empDAO.AddEmployee(emp);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}
	
	private String UpdateEmployee(ActionMapping mapping, ActionForm form) {
		EmployeeForm employee = (EmployeeForm) form;

		Employee emp = new Employee(employee.getName(),
				employee.getFirstName(), employee.getAddress(),
				employee.getPhoneNumber(), employee.getId());
		try {
			empDAO.UpdateEmployee(emp);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}
	
	private String DeleteEmployee(ActionMapping mapping, ActionForm form) {
		EmployeeForm employee = (EmployeeForm) form;

		Employee emp = new Employee(employee.getName(),
				employee.getFirstName(), employee.getAddress(),
				employee.getPhoneNumber(), employee.getId());
		try {
			empDAO.RemoveEmployee(emp);
		} catch (SQLException e) {
			return ACTION_FAIL;
		} catch (ClassNotFoundException e) {
			return ACTION_FAIL;
		}
		return ACTION_SUCCESS;
	}

	private ArrayList<Employee> GetEmployees() {
		try {
			return empDAO.GetEmployees();
		} catch (SQLException e) {
			return new ArrayList<Employee>();
		} catch (ClassNotFoundException e) {
			return new ArrayList<Employee>();
		}
	}
}